<?php
/**
 * 用户模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class UserAction extends CenterAction {
	
	public function shouju(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$model = M('Order');
		$order = $model->find($id);
		$this->assign('order',$order);
		$this->display();
	}
	
	public function pingzheng(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$model = M('Order');
		$order = $model->find($id);
		$this->assign('order',$order);
		$this->display();
	}
	
	public function index(){
		$type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : 1;
		$model = M('Order');
		$map['o.user_id'] = $_SESSION['UserInfo']['user_id'];
		if($type){
			$this->assign("type",$type);
			$map['o.order_type'] = $type;
		}
		$this->_list($model,$map,10,'add_time',false);
		
		$this->display();
	}
	
	public function order(){
		$id=isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '';
		$type=isset($_REQUEST['type']) && in_array($_REQUEST['type'],array(1,2,3)) ? intval($_REQUEST['type']) : '';
		$model=M('Article');
		$this->assign('hotel',$model->find($id));
		$this->assign('type',$type);
		if(!$type||!$id){
			$this->error('非法操作！');
		}
		$this->display();
	}
	
	public function doorder(){
		$id=isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '';
		$type=isset($_REQUEST['type']) ? intval($_REQUEST['type']) : '';
		$name=isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
		$start_date=isset($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : '';
		$end_date=isset($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';
		$card_number=isset($_REQUEST['card_number']) ? trim($_REQUEST['card_number']) : '';
		$mingdan=isset($_REQUEST['mingdan']) ? trim($_REQUEST['mingdan']) : '';
		$room_number=isset($_REQUEST['room_number']) ? intval($_REQUEST['room_number']) : '';
		$price=isset($_REQUEST['price']) ? trim($_REQUEST['price']) : '';
		$mobile=isset($_REQUEST['mobile']) ? trim($_REQUEST['mobile']) : '';
		$email=isset($_REQUEST['email']) ? trim($_REQUEST['email']) : '';
		if(empty($name)||empty($card_number)||empty($mobile)||empty($email)){
			$this->error('对不起，请输入完整信息！');
		}elseif($type==1 && (empty($start_date) || empty($end_date))){
			$this->error('对不起，请选择入住和退房日期！');
		}elseif($type==1 && (empty($room_number) || empty($mingdan))){
			$this->error('对不起，请输入完整信息！');
		}elseif(!regex($mobile,'mobile')){
			$this->error('对不起，手机号码不正确！');
		}elseif(!regex($email,'email')){
			$this->error('对不起，邮箱地址不正确！');
		}elseif($type==1 && $start_date >= $end_date){
			$this->error('对不起，入住时间应小于退房时间！');
		}
		$data=array(
			'user_id'		=>	$_SESSION['UserInfo']['user_id'],
			'art_id'		=>	$id,
			'order_type'	=>	$type,
			'name'			=>	$name,
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date,
			'card_number'	=>	$card_number,
			'room_number'	=>	$room_number,
			'mingdan'		=>	$mingdan,
			'pingzheng'		=>	'',
			'shouju'		=>	'',
			'price'			=>	$price,
			'mobile'		=>	$mobile,
			'email'			=>	$email,
			'add_time'		=>	time()
		);
		$model=M('Order');
		if($res=$model->add($data)){
			$this->success('恭喜您，预订成功！',__URL__.'/index');
		}else{
			$this->error('对不起，预订失败！');
		}
	}
	
	public function profile(){
		$model = M("User");
		if(IS_POST){
			$password	=	trim($_POST['password']);
			$confirm_password	=	trim($_POST['confirm_password']);
			$mobile	=	trim($_POST['mobile']);
			$email	=	trim($_POST['email']);
			//$code	=	trim($_POST['code']);
			$data=array(
				'id'		=>	$_SESSION['UserInfo']['user_id'],
				'mobile'	=>	$mobile,
				'email'		=>	$email,
			);
			if(!empty($password)){
				if(empty($confirm_password)){
					$this->error('请确认密码！');
				}elseif($confirm_password != $password){
					$this->error('两次输入的密码不一致！');
				}else{
					$data['password']	=	md5($password);
				}
			}elseif(empty($mobile)){
				$this->error('请填写手机号码！');
			}elseif(!regex($mobile,'mobile')){
				$this->error('手机号码不正确！');
			}elseif(empty($email)){
				$this->error('请填写邮箱地址！');
			}elseif(!regex($email,'email')){
				$this->error('邮箱地址不正确！');
			}
			//elseif($_SESSION['code'] != md5($code)){
				//$this->error('验证码错误！');
			//}
			$model = M("User");
			$res_id = $model->save($data);
			if($res_id){
				$this->success('修改成功！',__URL__."/profile");
			}else{
				$this->error('修改失败，也许你没有做任何修改！');
			}
		}
		$this->assign("user",$model->field("password",true)->find($_SESSION['UserInfo']['user_id']));
		$this->display();
	}
	
	protected function _list($model = '', $map = '', $page_size=15, $sortBy = '', $asc = false) {
        $model = empty($model) ? M($this->getActionName()) : $model;
        if (isset($_REQUEST['_order'])) {
            $order = 'a.'.$_REQUEST['_order'];
        } else {
            $order = !empty($sortBy) ? 'a.'.$sortBy : 'a.'.$model->getPk();
        }
        if (isset($_REQUEST['_sort'])) {
            $sort = $_REQUEST['_sort'] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        //记录数
        $count = $model->table(C("DB_PREFIX").'Order o')
        			->join('left join '.C('DB_PREFIX').'article a on(o.art_id=a.id)')
			    	->field('o.id')
			    	->where($map)
					->count('o.id');
        //echo $model->getLastSql();exit;
        if ($count > 0) {
            //分页显示
            import("ORG.Util.Page");
            $p = new Page($count, $page_size);
			//$p->rollPage=10;
            $page = $p->show();
			$list=$model->table(C("DB_PREFIX").'Order o')
					->join('left join '.C('DB_PREFIX').'article a on(o.art_id=a.id)')
			    	->field('o.*,a.title')
			    	->where($map)
			    	->order($order . ' ' . $sort)
			    	->limit($p->firstRow. ','.$p->listRows)
			    	->select();
			//echo $model->getLastSql();exit;
            $this->assign('page', $page);
			$new_list=array();
			foreach($list as $k=>$v){
				$new_list[$k]=$v;
				
			}
            $this->assign('list', $new_list);
        }
		return;
    }
	
}
?>
